using UnityEngine;
using System.Collections;



public class inventory : MonoBehaviour
{
    const int INVENTORY_SIZE = 6;
    // GUI
    Vector2 posBag = new Vector2(10, 10);
    public float slotSize = 30;
    public float spacingBetweenSlots = 3;
    public Texture2D defaultTextureSlot,tmpTexture;
    public Texture2D[] inventoryIconTextures ;
    public GUIStyle boxStyle;
    Texture mouseDragTexture; // this for drag & drop cursor
	
    // Data struct
    public int selectedItem = -1;
    bool[] flagAvailable; // marks which slot in the bag are available
    GameObject itemInHand; // mark which object is held in handg.
    GameObject[] itemsInBag; //represents all the objects in the bag
    bool itemPickedUpFlag = false; // track wether user has picked up a inventory item to place.
 

    


    void Start()
    {
        itemsInBag = new GameObject[INVENTORY_SIZE];
        flagAvailable = new bool[INVENTORY_SIZE];
        inventoryIconTextures = new Texture2D[INVENTORY_SIZE];
        for (int x = 0; x < INVENTORY_SIZE; x++)
        {
            itemsInBag[x] = null;
            flagAvailable[x] = true;
            inventoryIconTextures[x] = defaultTextureSlot;
        } 
    }

    void OnGUI()
    {
        Bag(); 
    }

    

    void Bag( )
    {
        tmpTexture = defaultTextureSlot;
        for (int curIdx = 0; curIdx < INVENTORY_SIZE; curIdx++)
        {
            if (itemsInBag[curIdx] == null)
            {//there's no object in this slot, set the default texture
                tmpTexture = defaultTextureSlot;
            }
            else
            {
                print("got one");
                tmpTexture = itemsInBag[curIdx].GetComponent<PropObjectInfo>().iconTexture;//set the object's texture	
            }
            inventoryIconTextures[curIdx] = tmpTexture;
        }
        float width = slotSize * itemsInBag.Length;
   
        selectedItem = GUI.SelectionGrid(new Rect(posBag.x, posBag.y, width, slotSize), selectedItem, inventoryIconTextures, INVENTORY_SIZE);
        if (selectedItem != -1 && itemsInBag[selectedItem] != null)
        {
            print(" you clickd on a inventory item");
			SetItemPickedUp(itemsInBag[selectedItem] );
			
        }
        else
        {
            print("you did not click on item ");
        }
    }


    public void SaveObjectInInventory(GameObject prop)
    {
		print (prop);
        for (int x = 0; x < INVENTORY_SIZE; x++)
        {
			print(x);
            if (itemsInBag[x] == null)
            {
				
                itemsInBag[x] = prop;
                flagAvailable[x] = false;
                prop.transform.localPosition = Vector3.zero;
                prop.active = false;
                prop.transform.parent = transform;

                break;
            }
        }
    }

	
	public bool IsItemPickedUp()
	{
			return itemPickedUpFlag;
	}
	
	public GameObject GetItemPickedUp()
	{
			return itemInHand;
	}
	
	public bool SetItemPickedUp(GameObject itemObj)
	{
		if (itemObj)
		{
			itemPickedUpFlag = true;
			itemInHand = itemObj;
			
		}
		else
		{
			itemPickedUpFlag = false;
			itemInHand = null;
		}
		return itemPickedUpFlag;
	}
}